//
//  priceRange.swift
//  uiKit-demo
//
//  Created by 陈澎 on 2025/1/10.
//

import UIKit
class PriceItem: UICollectionViewCell {
   
    let label: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.textAlignment = .center
        label.numberOfLines = 1
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.layer.cornerRadius = 14
        // 如果需要裁剪超出圆角的内容
        contentView.layer.masksToBounds = true
        contentView.addSubview(label)
        
        NSLayoutConstraint.activate([
            label.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
            label.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
        ])
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
class PriceRangeViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    let priceRange: [PriceRageItem] = [
            PriceRageItem(title: "我常购买", selected: true),
            PriceRageItem(title: "0-10元"),
            PriceRageItem(title: "10-20元"),
            PriceRageItem(title: "20-30元"),
            PriceRageItem(title: "30-40元")
    ]
    var tabCell: [PriceItem] = []

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return priceRange.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! PriceItem
        cell.label.text = priceRange[indexPath.item].title
        cell.contentView.backgroundColor = priceRange[indexPath.item].selected == true ?
        UIColor(red: 0.97, green: 0.32, blue: 0.37, alpha: 1) : UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1)
        cell.label.textColor = priceRange[indexPath.item].selected == true ? .white : .black
        tabCell.append(cell)
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let text = priceRange[indexPath.item].title
        let font = UIFont.systemFont(ofSize: 13)
        let maxSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: 28)
        let textSize = text.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil).size
        return CGSize(width: textSize.width + 36, height: 28) // 添加左右边距
    }
    // 点击tab处理
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath.row)
        for (idx, cell) in tabCell.enumerated() {
            if idx == indexPath.row {
                cell.contentView.backgroundColor = UIColor(red: 0.97, green: 0.32, blue: 0.37, alpha: 1)
                cell.label.textColor = .white
            } else {
                cell.contentView.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1)
                cell.label.textColor = .black
            }
        }
    }
    func render(parentView: UIView) {
        let priceRangeItem = UICollectionViewFlowLayout()
        // 列间距
        priceRangeItem.minimumInteritemSpacing = 9.5
        priceRangeItem.scrollDirection = .horizontal
        priceRangeItem.headerReferenceSize = CGSize(width: 12, height: 48)
        let priceRangeView = UICollectionView(
            frame: CGRect(x: 0, y: 39, width: view.bounds.width, height: 48),
            collectionViewLayout: priceRangeItem
        )
        priceRangeView.dataSource = self
        priceRangeView.delegate = self
        priceRangeView.backgroundColor = .white
        priceRangeView.register(PriceItem.self, forCellWithReuseIdentifier: "cell")
        priceRangeView.translatesAutoresizingMaskIntoConstraints = false
        // 隐藏水平滚动条
        priceRangeView.showsHorizontalScrollIndicator = false
        parentView.addSubview(priceRangeView)
    }
}
